mod tracking_allocator;
use std::sync::{Arc, Mutex};
use std::thread;

struct Node {
    next: Mutex<Option<Arc<Node>>>,
}

fn main() {
    let node1 = Arc::new(Node {
        next: Mutex::new(None),
    });
    
    let node2 = Arc::new(Node {
        next: Mutex::new(None),
    });

    // 线程1修改node1的next指向node2
    let node1_clone = Arc::clone(&node1);
    let node2_clone = Arc::clone(&node2);
    let handle1 = thread::spawn(move || {

        *node1_clone.next.lock().unwrap() = Some(node2_clone);
    });

    // 线程2修改node2的next指向node1
    let node1_clone = Arc::clone(&node1);
    let node2_clone = Arc::clone(&node2);
    let handle2 = thread::spawn(move || {

        *node2_clone.next.lock().unwrap() = Some(node1_clone);
    });

    handle1.join().unwrap();
    handle2.join().unwrap();

    // 此时引用计数：
    println!("node1 strong count: {}", Arc::strong_count(&node1)); // 输出2
    println!("node2 strong count: {}", Arc::strong_count(&node2)); // 输出2
}